package com.geek.novel.view.loading;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import com.geek.novel.R;


/**
 * Created by zzz40500 on 15/6/15.
 */
public class ShapeLoadingDialog extends Dialog{

    private LoadingView mLoadingView;

    private Builder mBuilder;

    private ShapeLoadingDialog(Builder builder) {
        super(builder.mContext, R.style.loading_dialog_style);
        mBuilder = builder;
        setCancelable(mBuilder.mCancelable);
        setCanceledOnTouchOutside(mBuilder.mCanceledOnTouchOutside);

//        //设置允许返回键关闭弹框(没什么卵用，还是需要思考怎么实现)
//        setOnKeyListener((arg0, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading_layout);

        mLoadingView = findViewById(R.id.loadView);

        mLoadingView.setDelay(mBuilder.mDelay);

        CharSequence text = curLoadingText;
        if(TextUtils.isEmpty(text)){
            text = mBuilder.mLoadText;
        }
        mLoadingView.setLoadingText(text);

        setOnDismissListener(dialog -> mLoadingView.setVisibility(View.GONE));
    }

    private static CharSequence curLoadingText;
    public void setText(CharSequence text){
        curLoadingText = text;
        if(mLoadingView != null){
            mLoadingView.setLoadingText(text);
        }
    }

    @Override
    public void show() {
        super.show();
        mLoadingView.setVisibility(View.VISIBLE);
    }

    public Builder getBuilder() {
        return mBuilder;
    }

    public static class Builder{

        private Context mContext;

        private int mDelay = 80;

        private CharSequence mLoadText = "加载中...";

        /**是否允许取消*/
        private boolean mCancelable = true;

        /**点击外部取消，默认是false*/
        private boolean mCanceledOnTouchOutside = false;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder delay(int delay) {
            mDelay = delay;
            return this;
        }

        public Builder loadText(CharSequence loadText) {
            mLoadText = loadText;
            return this;
        }

        public Builder loadText(int resId) {
            mLoadText = mContext.getString(resId);
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            mCancelable = cancelable;
            mCanceledOnTouchOutside = cancelable;
            return this;
        }

        public Builder canceledOnTouchOutside(boolean canceledOnTouchOutside) {
            mCanceledOnTouchOutside = canceledOnTouchOutside;
            return this;
        }

        public ShapeLoadingDialog build(){
            return new ShapeLoadingDialog(this);
        }

    }
}
